//
//  PhotosView.m
//  微博登录
//
//  Created by tarena on 16/10/21.
//  Copyright © 2016年 tarena. All rights reserved.
//
#import "UIImageView+AFNetworking.h"
#import "PhotosView.h"
#import "SDPhotoBrowser.h"

#define kSpacing 8

@implementation PhotosView

-(void)setPic_urls:(NSArray *)pic_urls{
     NSLog(@"%@",pic_urls);
    _pic_urls = pic_urls;
    
    //清空
    for (UIImageView *iv in self.subviews) {
        [iv removeFromSuperview];
    }
    
    float size = (self.width-2*kSpacing)/3;//([UIScreen mainScreen].bounds.size.width-50-20-4*8)/3;
    for (int i=0; i<pic_urls.count; i++) {
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i%3*(size+kSpacing), i/3*(size+kSpacing), size, size)];
        NSString *imagePath = pic_urls[i];
        iv.tag = i;
        [iv setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed:@"我的背景"]];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        //超出内容不显示
        iv.clipsToBounds = YES;
        [self addSubview:iv];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [iv addGestureRecognizer:tap];
        iv.userInteractionEnabled = YES;
    }
}
//*****************************
-(void)tapAction:(UITapGestureRecognizer *)tap{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self; // 原图的父控件
    browser.imageCount = self.pic_urls.count; // 图片总数
    browser.currentImageIndex = tap.view.tag;
    browser.delegate = self;
    [browser show];
}
//************************
#pragma mark - photobrowser代理方法

// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}

//http://www.abc.com/images/0_1_small.jpg
//http://www.abc.com/images/0_1_big.jpg

// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = self.pic_urls[index];
//   urlStr = [urlStr stringByReplacingOccurrencesOfString:@"small" withString:@"big"];
    return [NSURL URLWithString:urlStr];
}
@end
